/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"
#include "cut.h"
#include "jukuapp.h"
#include <iostream>

Cut::Cut() : m_pos(0,0)
{
	//std::cout<<"Cut"<<std::endl;
}

Cut::~Cut()
{
	//std::cout<<"~Cut"<<std::endl;
}

void Cut::WriteXML(TiXmlNode* root)
{
    TiXmlElement *element = new TiXmlElement("Cut");
    root->LinkEndChild(element);
    element->SetAttribute("x", m_pos.m_x);
    element->SetAttribute("y", m_pos.m_y);
	WriteBaseXML(element);
}

JukuObject* Cut::ReadFromXMLElement(TiXmlElement* element)
{
	//std::cout<<"Cut::ReadFromXMLElement()"<<std::endl;

    Cut *cut = new Cut();

	element->Attribute("x", &cut->m_pos.m_x);
	element->Attribute("y", &cut->m_pos.m_y);

	cut->ReadBaseXML(element);

    return cut;
}

void Cut::Draw(wxDC &dc, wxWindow *window, wxPoint origin)
{
	int xadd = m_pos.m_x + origin.x;
	int yadd = m_pos.m_y + origin.y;
	if(GetSelected()){
		wxPen pen(wxGetApp().m_settings.m_colour_selected,
				wxGetApp().m_settings.m_linewidth_cut);
		dc.SetPen(pen);
	}
	else{
		wxPen pen;
		if(GetAutomatic()){
			pen = wxPen(wxGetApp().m_settings.m_colour_automatic,
					wxGetApp().m_settings.m_linewidth_cut);
		}
		else{
			pen = wxPen(wxGetApp().m_settings.m_colour_cut,
					wxGetApp().m_settings.m_linewidth_cut);
		}
		dc.SetPen(pen);
	}
	wxBrush brush(*wxBLACK, wxTRANSPARENT);
	dc.SetBrush(brush);
	/*dc.DrawLine(-40+xadd,-40+yadd, 40+xadd, 40+yadd);
	dc.DrawLine(-40+xadd, 40+yadd, 40+xadd,-40+yadd);*/
	dc.DrawLine(-30+xadd,-30+yadd, 30+xadd, 30+yadd);
	dc.DrawLine(-30+xadd, 30+yadd, 30+xadd,-30+yadd);
	/*if(GetAutomatic()){
		dc.DrawLine(-30+xadd,-30+yadd, 30+xadd,-30+yadd); //top
		dc.DrawLine(-30+xadd, 30+yadd, 30+xadd, 30+yadd); //bottom
		dc.DrawLine(-30+xadd,-30+yadd,-30+xadd, 30+yadd); //left
		dc.DrawLine( 30+xadd, 30+yadd, 30+xadd,-30+yadd); //right
	}*/
}

BoardRect Cut::GetDrawArea()
{
	return BoardRect(-70+m_pos.m_x, -70+m_pos.m_y, 140, 140);
}

BoardPoint Cut::GetBoardPos()
{
	return m_pos;
}

void Cut::SetBoardPos(BoardPoint p)
{
	m_pos = p;
}

void Cut::Move(int dx, int dy)
{
	m_pos.m_x += dx;
	m_pos.m_y += dy;
}

bool Cut::IsSelectedInPoint(int x, int y)
{
	return (abs(x - m_pos.m_x) < 50 && abs(y - m_pos.m_y) < 50);
}

bool Cut::IsSelectedInArea(int x1, int y1, int x2, int y2, bool touch_is_enough)
{
	BoardRect sr = BoardRect(x1, y1, x2-x1, y2-y1);
	BoardRect r(m_pos.m_x - 50, m_pos.m_y - 50, 100, 100);
	if(touch_is_enough){
		if(r.Collides(sr)) return true;
	}
	else{
		if(sr.Contains(r)) return true;
	}
	return false;
}

/*void Cut::SetSelected(bool selected)
{
	JukuObject::SetSelected(selected);
}*/

void Cut::Rotate(TurnDirection dir, BoardPoint origin)
{
	int ox = m_pos.m_x - origin.m_x;
	int oy = m_pos.m_y - origin.m_y;
	int nox, noy;
	if(dir==CCW){
		nox = oy;
		noy = -ox;
	}
	else{ //CW
		nox = -oy;
		noy = ox;
	}
	m_pos.m_x = origin.m_x + nox;
	m_pos.m_y = origin.m_y + noy;
}

BoardRect Cut::GetPinExtents()
{
	return BoardRect(m_pos.m_x, m_pos.m_y, 0, 0);
}
